package com.baseweb.webadmin.controller.app;

import com.baseweb.webadmin.controller.BaseController;
import com.baseweb.webadmin.core.constants.AdPositionTypeNow;
import com.baseweb.webadmin.core.model.bvo.ByAdvObjVo;
import com.baseweb.webadmin.core.model.pojo.AppUserList;
import com.baseweb.webadmin.core.model.pojo.ByAd;
import com.baseweb.webadmin.core.model.pojo.ByChannel;
import com.baseweb.webadmin.core.model.pojo.User;
import com.baseweb.webadmin.core.util.RequestPage;
import com.baseweb.webadmin.core.util.ResultPage;
import com.baseweb.webadmin.service.ContentService;
import com.baseweb.webadmin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by dengsy on 2017/8/16.
 */
@Controller
@RequestMapping(value = "/appApiIndex")
public class IndexAppController extends BaseController {
    @Autowired
    private ContentService contentService;

    @Autowired
    private UserService userService;

    /**
     * 主页面顶部小图轮播查询
     */
    @ResponseBody
    @RequestMapping(value = "/getCenterBottom", method = RequestMethod.GET)
    public Map<String, Object> getCenterBottom(){
//        List<ByAd> centerBottomList = contentService.findAllByAdByPositionId(AdPositionTypeNow.LIST_ADSMALL.getCode(),4,"");
//        List<ByAdvObjVo> advObjVo = new ArrayList<ByAdvObjVo>();
//        if (centerBottomList != null && centerBottomList.size() > 0) {
//            for (ByAd ad : centerBottomList) {
//                ByAdvObjVo v = new ByAdvObjVo();
//                v.setAd(ad);
//                v.setChildren(contentService.findAllByAdByPrarentId(ad.getId(), 1000,null));
//                advObjVo.add(v);
//            }
//        }
        List<ByAd> centerLunboList = contentService.findAllByAdByPositionId(AdPositionTypeNow.INDEX_BIGADLUNBO.getCode(),3,"");
        List<ByAdvObjVo> advObjVo = new ArrayList<ByAdvObjVo>();
        if (centerLunboList != null && centerLunboList.size() > 0) {
            for (ByAd ad : centerLunboList) {
                ByAdvObjVo v = new ByAdvObjVo();
                v.setAd(ad);
                v.setChildren(contentService.findAllByAdByPrarentId(ad.getId(), 1000,null));
                advObjVo.add(v);
            }
        }
        return success(advObjVo,"查询成功");
    }

    /**
     * 查询第三级广告列表
     * @param parentId
     * @param advType
     * @param city
     * @return
     */
    @RequestMapping(value = "/getAdvThreeData")
    @ResponseBody
    public Map<String, Object> loadAdvThreeData(Integer parentId, Integer advType, String city) {
        List<ByAd> list = new ArrayList<>();
//        if(advType!=null&&(advType == 1 || advType == 2 || advType == 3)){
            list = contentService.findAllByParentId(parentId, 1000, "");
//        }else {
//            list = contentService.findAllByParentId(parentId, 1000, city==null?"":city);
//        }
        return success(list,"广告列表查询成功");
    }

    /**
     * 根据关键字查询关键字用户
     */
    @RequestMapping(value = "/findUserByKey", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findAllUserChannelByUserId(@ModelAttribute()AppUserList appUserList,@RequestParam("channelId") String channelId, RequestPage requestPage){
        requestPage = requestPage == null ? new RequestPage() : requestPage;
        String startPrice = "0";
        String endPrice = "99999999";
        String orders = "id ";
        String wedding = "";
        String cnName="%%";
        StringBuffer channelIds = new StringBuffer();
        if(appUserList.getWedding() != null){
            wedding = appUserList.getWedding();
        }
        if(appUserList.getPrice_type() != null){
            if("1".equals(appUserList.getPrice_type())){
                startPrice = "0";
                endPrice = "5000";
            }else if("2".equals(appUserList.getPrice_type())){
                startPrice = "5000";
                endPrice = "10000";
            }else if("3".equals(appUserList.getPrice_type())){
                startPrice = "10000";
                endPrice = "20000";
            }else if("4".equals(appUserList.getPrice_type())){
                startPrice = "20000";
                endPrice = "30000";
            }else if("5".equals(appUserList.getPrice_type())){
                startPrice = "30000";
                endPrice = "50000";
            }else if("6".equals(appUserList.getPrice_type())){
                startPrice = "50000";
                endPrice = "99999999";
            }
        }
        if(appUserList.getOrder() != null){
            if("biz_view_count".equals(appUserList.getOrder())){
                orders = "biz_view_count,id ";
            }else if("biz_view_count_desc".equals(appUserList.getOrder())){
                orders = "biz_view_count,id desc ";
            }else if("biz_star".equals(appUserList.getOrder())){
                orders = "biz_star,id ";
            }else if("biz_star_desc".equals(appUserList.getOrder())){
                orders = "biz_star,id desc ";
            }else if("biz_price".equals(appUserList.getOrder())){
                orders = "biz_price,id ";
            }else if("biz_price_desc".equals(appUserList.getOrder())){
                orders = "biz_price,id desc ";
            }
        }
        if(appUserList.getCn_name()!=null){
            cnName = "%"+appUserList.getCn_name()+"%";
        }
        String[] strArray =  channelId.split(",");
        for(int i=0;i<strArray.length;i++){
            channelIds.append(strArray[i]+",");
        }
        channelIds.deleteCharAt(channelIds.length() - 1);
        List<AppUserList> userList = userService.findAllUserChannelByUserIdAPP(appUserList.getArea(),startPrice,endPrice,wedding,orders,requestPage.getStart(),requestPage.getPageSize(),appUserList.getRole_type_id(),cnName,channelIds.toString());
        //总记录数
        int numSum = userService.countUserChannelByUserIdAPP(appUserList.getArea(),startPrice,endPrice,wedding,orders,appUserList.getRole_type_id(),cnName,channelIds.toString());
        ResultPage userPage = new ResultPage<>(numSum,requestPage.getPageSize(),requestPage.getCurPage(),userList);
        return success(userPage,"查询成功");
    }

    /**
     * 首页导航栏
     */
    @ResponseBody
    @RequestMapping(value = "/getNavigation", method = RequestMethod.GET)
    public Map<String, Object> getNavigation(){
        List<ByChannel> topList = contentService.findAllByChannelByPositionId(2);
        return success(topList,"查询成功");
    }

    /**
     * 主页策划师查询
     */
    @ResponseBody
    @RequestMapping(value = "/getPlanList", method = RequestMethod.POST)
    public Map<String, Object> getPlanList(@RequestParam(value = "city", defaultValue = "成都市") String city){
        List<ByAd> centerList = contentService.findAllByAdByPrarentId(8, 3,city);
        List<ByAdvObjVo> advObjVo = new ArrayList<ByAdvObjVo>();
        if (centerList != null && centerList.size() > 0) {
            for (ByAd ad : centerList) {
                ByAdvObjVo v = new ByAdvObjVo();
                v.setAd(ad);
                v.setChildren(contentService.findAllByAdByPrarentId(ad.getId(), 6,city));
                advObjVo.add(v);
            }
        }
        return success(advObjVo,"查询成功");
    }

    /**
     * 主页主持查询
     */
    @ResponseBody
    @RequestMapping(value = "/getHostList", method = RequestMethod.POST)
    public Map<String, Object> getHostList(@RequestParam(value = "city", defaultValue = "成都市") String city){
        List<ByAd> centerList = contentService.findAllByAdByPrarentId(13, 3, city);
        List<ByAdvObjVo> advObjVo = new ArrayList<ByAdvObjVo>();
        if (centerList != null && centerList.size() > 0) {
            for (ByAd ad : centerList) {
                ByAdvObjVo v = new ByAdvObjVo();
                v.setAd(ad);
                v.setChildren(contentService.findAllByAdByPrarentId(ad.getId(), 6, city));
                advObjVo.add(v);
            }
        }
        return success(advObjVo,"查询成功");
    }

    /**
     * 主页摄影师查询
     */
    @ResponseBody
    @RequestMapping(value = "/getPhotographerList", method = RequestMethod.POST)
    public Map<String, Object> getPhotographerList(@RequestParam(value = "city", defaultValue = "成都市") String city){
        List<ByAd> centerList = contentService.findAllByAdByPrarentId(18, 3, city);
        List<ByAdvObjVo> advObjVo = new ArrayList<ByAdvObjVo>();
        if (centerList != null && centerList.size() > 0) {
            for (ByAd ad : centerList) {
                ByAdvObjVo v = new ByAdvObjVo();
                v.setAd(ad);
                v.setChildren(contentService.findAllByAdByPrarentId(ad.getId(), 6, city));
                advObjVo.add(v);
            }
        }
        return success(advObjVo,"查询成功");
    }

    /**
     * 主页化妆师查询
     */
    @ResponseBody
    @RequestMapping(value = "/getMakeupList", method = RequestMethod.POST)
    public Map<String, Object> getMakeupList(@RequestParam(value = "city", defaultValue = "成都市") String city){
        List<ByAd> centerList = contentService.findAllByAdByPrarentId(28, 3, city);
        List<ByAdvObjVo> advObjVo = new ArrayList<ByAdvObjVo>();
        if (centerList != null && centerList.size() > 0) {
            for (ByAd ad : centerList) {
                ByAdvObjVo v = new ByAdvObjVo();
                v.setAd(ad);
                v.setChildren(contentService.findAllByAdByPrarentId(ad.getId(), 6, city));
                advObjVo.add(v);
            }
        }
        return success(advObjVo,"查询成功");
    }

    /**
     * 主页摄像师查询
     */
    @ResponseBody
    @RequestMapping(value = "/getCameraList", method = RequestMethod.POST)
    public Map<String, Object> getCameraList(@RequestParam(value = "city", defaultValue = "成都市") String city){
        List<ByAd> centerList = contentService.findAllByAdByPrarentId(23, 3, city);
        List<ByAdvObjVo> advObjVo = new ArrayList<ByAdvObjVo>();
        if (centerList != null && centerList.size() > 0) {
            for (ByAd ad : centerList) {
                ByAdvObjVo v = new ByAdvObjVo();
                v.setAd(ad);
                v.setChildren(contentService.findAllByAdByPrarentId(ad.getId(), 6, city));
                advObjVo.add(v);
            }
        }
        return success(advObjVo,"查询成功");
    }
}
